package com.cfushn.ddd.tools.string;

import java.util.List;

/**
 * sql 值拼接
 *
 * @author CFuShn 2021/11/30 14:55
 */
@SuppressWarnings("all")
public abstract class LineBuilder<Children extends LineBuilder<Children>> {

    protected final Children typedThis = (Children) this;
    protected StringBuilder thisBuilder;
    protected int lineCount;
    protected boolean isLineEnd = false;

    protected LineBuilder() {
        thisBuilder = new StringBuilder();
    }

    /**
     * 拼接列名或值
     *
     * @param s s
     * @return this
     */
    public abstract Children append(Object s);

    /**
     * 拼接列名或值
     *
     * @param list list
     * @return this
     */
    public Children appendList(List<?> list) {
        list.forEach(typedThis::append);
        isLineEnd = false;
        return typedThis;
    }

    /**
     * 结束一行,并括上
     *
     * @return this
     */
    public Children line() {
        thisBuilder.append(")");
        isLineEnd = true;
        lineCount++;
        return typedThis;
    }

    @Override
    public String toString() {
        return thisBuilder.toString();
    }

    public int length() {
        return thisBuilder.length();
    }

    public int lineCount() {
        return lineCount;
    }

    public void clear() {
        thisBuilder.delete(0, thisBuilder.length());
        lineCount = 0;
        isLineEnd = false;
    }
}
